//
//  CompleteOrderDetailsViewController.m
//  YaDiuDiu
//
//  Created by 陈刚 on 2017/10/12.
//  Copyright © 2017年 jy. All rights reserved.
//

#import "CompleteOrderDetailsViewController.h"
#import "RentalDetailsTableViewCell.h"

@interface CompleteOrderDetailsViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (weak, nonatomic) IBOutlet UIScrollView *bgScrollView;

@property (weak, nonatomic) IBOutlet UITableView *completeTable;

@property (weak, nonatomic) IBOutlet UILabel *contentLabel;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *tableHeightc;

@property (strong, nonatomic) StarView *starView;

@property (weak, nonatomic) IBOutlet UIButton *tellPhoneButton;

@property (nonatomic, strong) NSMutableArray *titleArray;

@property (nonatomic, strong) NSMutableArray *dataArray;

@property (nonatomic, strong) NSMutableArray *urlArray;

- (IBAction)tellPhoneEvent:(UIButton *)sender;

@property (weak, nonatomic) IBOutlet UIView *comBottomView;

@end

@implementation CompleteOrderDetailsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupView];
    
    [self loadData];
    // Do any additional setup after loading the view from its nib.
}

- (void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];
    [self.bgScrollView setContentSize:CGSizeMake(ScreenWidth, self.tableHeightc.constant + 130)];
}

- (void)setupView{
    [self showNav:@"订单详情"];
    
    [self.completeTable registerNib:[UINib nibWithNibName:@"RentalDetailsTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"Cell"];
    
    self.urlArray = [[NSMutableArray alloc]initWithObjects:@"",@"/App/Ordermanage/buycarorderdetails",@"/App/Ordermanage/cawashorderdetails",@"/App/Ordermanage/rentorderdatails",@"/App/Ordermanage/ontheblesdetails",@"/App/Ordermanage/cooliedetails",@"/App/Ordermanage/detailscleaning",@"/App/Ordermanage/housekeepingdetails",@"/App/Ordermanage/lockorderdetails",@"/App/Ordermanage/vegetablesdetails", nil];
    self.starView = [[StarView alloc]initWithFrame:CGRectMake(15, 43, 150, 15)];
    [self.comBottomView addSubview:self.starView];
    
    NSInteger type = [self.businessId integerValue];
    switch (type) {
        case 1:
            self.titleArray = [[NSMutableArray alloc]initWithObjects:@"订 单 号",@"下单时间",@"车型名称",@"取货地址", nil];
            break;
        case 2:
            self.titleArray = [[NSMutableArray alloc]initWithObjects:@"订 单 号",@"下单时间",@"车型名称",@"取货地址", nil];
            break;
        case 3:
            self.titleArray = [[NSMutableArray alloc]initWithObjects:@"订 单 号",@"下单时间",@"楼盘名称",@"取货地址", nil];
            break;
        case 4:
            self.titleArray = [[NSMutableArray alloc]initWithObjects:@"订 单 号",@"下单时间",@"车辆品牌",@"车    型",@"排    量",@"上户费用",@"上户区域",@"车主电话",@"车辆发票",@"合 格 证",@"身 份 证", nil];
            break;
        case 5:
            self.titleArray = [[NSMutableArray alloc]initWithObjects:@"订 单 号",@"下单时间",@"大概重量",@"取货地址",@"配送地址",@"物品描述", nil];
            break;
        case 6:
            self.titleArray = [[NSMutableArray alloc]initWithObjects:@"订 单 号",@"下单时间",@"商家名称",@"取货地址", nil];
            break;
        case 7:
            self.titleArray = [[NSMutableArray alloc]initWithObjects:@"订 单 号",@"下单时间",@"商家名称",@"取货地址", nil];
            break;
        case 8:
            self.titleArray = [[NSMutableArray alloc]initWithObjects:@"订 单 号",@"下单时间",@"取货地址", nil];
            break;
        case 9:
            self.titleArray = [[NSMutableArray alloc]initWithObjects:@"订 单 号",@"下单时间",@"取货地址", nil];
            break;
            
        default:
            break;
    }
}

- (void)loadData{
    
    [SVProgressHUD show];
    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
    NSMutableDictionary *parmas = [NSMutableDictionary dictionary];
    NSInteger index = [self.businessId integerValue];
    NSString *urlString = [self.urlArray objectAtIndex:index];
    NSString *idString = [self.valueDic objectForKey:@"id"];
    [parmas setValue:[user objectForKey:@"userid"] forKey:@"userid"];
    [parmas setValue:[user objectForKey:@"userid"] forKey:@"merchantid"];
    [parmas setValue:idString forKey:@"id"];
    NSLog(@"parmas:%@",parmas);
    NSString *url = [NSString stringWithFormat:@"%@%@",urlHead,urlString];
    [[AFHTTPSessionManager manager] POST:url parameters:parmas success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"res:%@",[responseObject objectForKey:@"mssage"]);
        NSInteger error = [[responseObject objectForKey:@"error"] integerValue];
        NSString *mssage = [responseObject objectForKey:@"mssage"];
        NSDictionary *resultDic = [responseObject objectForKey:@"result"];
        if (error == 200 && [resultDic isKindOfClass:[NSDictionary class]]) {
            [self bindData:resultDic];
            [self.completeTable reloadData];
            self.tableHeightc.constant = self.dataArray.count * 44 + 44;
            NSDictionary *commentDic = [resultDic objectForKey:@"comment"];
            NSInteger startCount = [[commentDic objectForKey:@"star"] integerValue];
            self.starView.showStar = startCount * 20;
            NSString *comment = [commentDic objectForKey:@"comment"];
            [self.contentLabel setText:comment];
            [SVProgressHUD dismiss];
        }else{
            [SVProgressHUD showErrorWithStatus:mssage];
        }
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"error:%@",error);
        [SVProgressHUD dismiss];
    }];
}

- (void)bindData:(NSDictionary *)dataDic{
    NSInteger type = [self.businessId integerValue];
    
    
    
    switch (type) {
        case 1:
        case 2:
        case 3:{
            NSString *ordernumber = [dataDic objectForKey:@"ordernumber"];
            NSString *addtime = [dataDic objectForKey:@"addtime"];
            NSString *orderTime = [self getyYearsTimeFormat:addtime];
            NSString *ordername = [dataDic objectForKey:@"ordername"];
            NSString *phone = [dataDic objectForKey:@"phone"];
            self.dataArray = [[NSMutableArray alloc]initWithObjects:ordernumber,orderTime,ordername,phone, nil];
        }
            break;
        case 4:{
            NSDictionary *detailsDic = [dataDic objectForKey:@"details"];
            NSString *ordernumber = [detailsDic objectForKey:@"ordernumber"];
            NSString *addtime = [detailsDic objectForKey:@"addtime"];
            NSString *orderTime = [self getyYearsTimeFormat:addtime];
            NSString *brand = [detailsDic objectForKey:@"brand"];
            NSString *models = [detailsDic objectForKey:@"models"];
            NSString *displacement = [detailsDic objectForKey:@"displacement"];
            NSString *ordermoney = [detailsDic objectForKey:@"ordermoney"];
            NSString *userarea = [detailsDic objectForKey:@"userarea"];
            NSString *ownerphone = [detailsDic objectForKey:@"ownerphone"];
            NSString *invoice = [detailsDic objectForKey:@"invoice"];
            NSString *certificate = [detailsDic objectForKey:@"certificate"];
            NSString *idcardone = [detailsDic objectForKey:@"idcardone"];
            self.dataArray = [[NSMutableArray alloc]initWithObjects:ordernumber,orderTime,brand,models,displacement,ordermoney,userarea,ownerphone,invoice,certificate,idcardone, nil];
        }
            break;
        case 5:{
            self.titleArray = [[NSMutableArray alloc]initWithObjects:@"订 单 号",@"下单时间",@"大概重量",@"取货地址",@"配送地址",@"物品描述", nil];
            NSString *ordernumber = [dataDic objectForKey:@"ordernumber"];
            NSString *addtime = [dataDic objectForKey:@"addtime"];
            NSString *orderTime = [self getyYearsTimeFormat:addtime];
            NSString *ordername = [dataDic objectForKey:@"ordername"];
            NSString *weight = [dataDic objectForKey:@"weight"];
            NSString *peiAddress = [NSString stringWithFormat:@"%@ %@/n%@ %@",[dataDic objectForKey:@"ordername"],[dataDic objectForKey:@"phone"],[dataDic objectForKey:@"quaddress"],[dataDic objectForKey:@"quhousnum"]];
            NSString *showAddress = [NSString stringWithFormat:@"%@ %@/n%@ %@",[dataDic objectForKey:@"goname"],[dataDic objectForKey:@"gophone"],[dataDic objectForKey:@"goaddress"],[dataDic objectForKey:@"gonum"]];
            self.dataArray = [[NSMutableArray alloc]initWithObjects:ordernumber,orderTime,ordername,weight,peiAddress,showAddress, nil];
        }
            break;
        case 6:{
            NSString *ordernumber = [dataDic objectForKey:@"ordernumber"];
            NSString *addtime = [dataDic objectForKey:@"addtime"];
            NSString *orderTime = [self getyYearsTimeFormat:addtime];
            NSString *distname = [dataDic objectForKey:@"distname"];
            NSString *address = [NSString stringWithFormat:@"%@ %@\n %@",[dataDic objectForKey:@"distname"],[dataDic objectForKey:@"disttel"],[dataDic objectForKey:@"address"]];
            self.dataArray = [[NSMutableArray alloc]initWithObjects:ordernumber,orderTime,distname,address, nil];
        }
            break;
        case 7:{
            NSString *ordernumber = [dataDic objectForKey:@"ordernumber"];
            NSString *addtime = [dataDic objectForKey:@"addtime"];
            NSString *orderTime = [self getyYearsTimeFormat:addtime];
            NSString *ordername = [dataDic objectForKey:@"distname"];
            NSString *address = [NSString stringWithFormat:@"%@ %@\n %@",[dataDic objectForKey:@"consignee"],[dataDic objectForKey:@"tel"],[dataDic objectForKey:@"address"]];
            self.dataArray = [[NSMutableArray alloc]initWithObjects:ordernumber,orderTime,ordername,address, nil];
        }
            break;
        case 8:{
            NSString *ordernumber = [dataDic objectForKey:@"ordernumber"];
            NSString *addtime = [dataDic objectForKey:@"addtime"];
            NSString *orderTime = [self getyYearsTimeFormat:addtime];
            NSString *address = [NSString stringWithFormat:@"%@ %@\n %@",[dataDic objectForKey:@"person"],[dataDic objectForKey:@"tel"],[dataDic objectForKey:@"detailedaddress"]];
            self.dataArray = [[NSMutableArray alloc]initWithObjects:ordernumber,orderTime,address, nil];
        }
            
            break;
        case 9:{
            NSString *ordernumber = [dataDic objectForKey:@"ordernumber"];
            NSString *addtime = [dataDic objectForKey:@"addtime"];
            NSString *orderTime = [self getyYearsTimeFormat:addtime];
            NSString *address = [NSString stringWithFormat:@"%@ %@\n %@",[dataDic objectForKey:@"consignee"],[dataDic objectForKey:@"phone"],[dataDic objectForKey:@"address"]];
            self.dataArray = [[NSMutableArray alloc]initWithObjects:ordernumber,orderTime,address, nil];
        }
            break;
            
        default:
            break;
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *Identitfer  = @"Cell";
    RentalDetailsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Identitfer];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"RentalDetailsTableViewCell" owner:nil options:nil] lastObject];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    [cell.itemLabel setText:[self.titleArray objectAtIndex:indexPath.row]];
    [cell.valueLabel setText:[self.dataArray objectAtIndex:indexPath.row]];
    return cell;
}

- (NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [[NSMutableArray alloc]init];
    }
    return _dataArray;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (IBAction)tellPhoneEvent:(UIButton *)sender {
    
}
@end
